"use strict";

import axios from "axios";
import config1 from "@/config/config";
import {showNotify} from "vant";

// Full config:  https://github.com/axios/axios#request-config
// axios.defaults.baseURL = process.env.baseURL || process.env.apiUrl || '';
// axios.defaults.headers.common['Authorization'] = AUTH_TOKEN;
// axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';

let config = {
  baseURL:  config1.baseUrl ,
    timeout: 1000 * 30,
    withCredentials: true,
    headers: {
        'Content-Type': 'application/json; charset=utf-8'
    }
};

const _axios = axios.create(config);

_axios.interceptors.request.use(
  function(config) {
    // Do something before request is sent
    return config;
  },
  function(error) {
    // Do something with request error
    return Promise.reject(error);
  }
);

// Add a response interceptor
_axios.interceptors.response.use(
  function(response) {
    // Do something with response data
    return response;
  },
  function(error) {
    // Do something with response error
    return Promise.reject(error);
  }
);
//
// Plugin.install = function(Vue, options) {
//   Vue.axios = _axios;
//   window.axios = _axios;
//   Object.defineProperties(Vue.prototype, {
//     axios: {
//       get() {
//         return _axios;
//       }
//     },
//     $axios: {
//       get() {
//         return _axios;
//       }
//     },
//   });
// };

const baseURL = config1.baseUrl
const Axios = {
}
 Object.defineProperties(Axios, {
     post:{
         get() {
             return (url, data,flag,qq) => {
                 axios.post(baseURL+url, data).then(res => {
                     if (res.data.code == 1) {
                         showNotify({type: 'success', message: res.data.data});

                         if(flag==1){
                             window.localStorage.setItem("qq",qq)
                             window.location.href='/index'
                         }
                     } else showNotify({type: 'danger', message: res.data.msg});
                 }).catch(err => {
                     showNotify({type: 'danger', message: 网络异常});
                 })
             }
         }
  },
     get: {
         get() {
             return function (url, data) {
                 axios.get(baseURL+url,{
                         params: data
                     }
                 ).then(res => {
                     if (res.data.code == 1) {
                         showNotify({type: 'success', message: res.data.data});
                     } else showNotify({type: 'danger', message: res.data.msg});
                 }).catch(err => {
                     showNotify({type: 'danger', message:"网络异常"});
                 })

             }
         }
     },



 })


export default Axios;
